package com.jianjun.project.monitor.job;


import com.jfinal.aop.Before;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.jianjun.project.common.model.SysJobLog;

import java.util.Date;
import java.util.List;

/**
 * 定时任务调度日志信息 服务层
 *
 * @author source
 */

public class SysJobLogService {
    private final SysJobLog dao = new SysJobLog().dao();

    public Page<SysJobLog> paginate(SysJobLog model) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysJobLog", Kv.by("cond", model.toMap()).set(model.getColumnAsc()));
        return dao.paginate(model.getPageNum(), model.getPageSize(), sqlParaDb);
    }


    /**
     * 获取quartz调度器日志的计划任务
     */
    public List<SysJobLog> selectJobLogList(Model<SysJobLog> model) {
        SqlPara sqlParaDb = Db.getSqlPara("querySysJobLog", Kv.by("cond", model.toMap()));
        return dao.find(sqlParaDb);
    }

    /**
     * 通过调度任务日志ID查询调度信息
     *
     * @param jobLogId 调度任务日志ID
     * @return 调度任务日志对象信息
     */
    public SysJobLog selectJobLogById(Long jobLogId) {
        return dao.findById(jobLogId);
    }

    /**
     * 新增任务日志
     *
     * @param jobLog 调度日志信息
     */
    public void addJobLog(SysJobLog jobLog) {
        jobLog.setCreateTime(new Date());
        jobLog.save();
    }


    /**
     * 批量删除调度日志信息
     *
     * @param logIds 需要删除的数据ID
     * @return 结果
     */
    @Before(Tx.class)
    public boolean deleteJobLogByIds(Long[] logIds) {
        for (Long id : logIds) {
            dao.deleteById(id);
        }
        return true;
    }

    /**
     * 清空任务日志
     */
    public void cleanJobLog() {
        Db.template("deleteJobLog").update();
    }
}
